package com.example.wangning.mycarofcountry.db;

import com.example.mycarofcountry.entity.dao.DaoSession;
import com.example.mycarofcountry.entity.dao.FamousList;
import com.example.mycarofcountry.entity.dao.Forum_total;
import com.example.mycarofcountry.entity.dao.JingXuanList;
import com.example.mycarofcountry.entity.dao.Recommend_Evaluating;
import com.example.mycarofcountry.entity.dao.Recommend_Focusing;
import com.example.mycarofcountry.entity.dao.Recommend_News;
import com.example.mycarofcountry.entity.dao.Recommend_Newslist;
import com.example.mycarofcountry.entity.dao.Recommend_Technologylist;
import com.example.mycarofcountry.entity.dao.Recommend_Vehiclelist;
import com.example.mycarofcountry.entity.dao.ReduceCarList;
import com.example.mycarofcountry.entity.dao.ReduceDealer;
import com.example.mycarofcountry.entity.dao.ReduceDealerDao;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 16/1/18.
 */
public class DaoHelper {
    private static DaoHelper helper;
    private static DaoSession daoSession;

    private DaoHelper() {
    }

    public static DaoHelper getInstance() {
        if (helper == null) {
            helper = new DaoHelper();
            daoSession = BackDao.getDaoSession();
        }
        return helper;
    }
    public void deleVehiclelist(){
        daoSession.getRecommend_VehiclelistDao().deleteAll();
    }
    public void addVehiclelist(List<Recommend_Vehiclelist> data){
        deleVehiclelist();
        daoSession.getRecommend_VehiclelistDao().insertInTx(data);
    }
    public List<Recommend_Vehiclelist> searchVehiclelist(){
        return daoSession.getRecommend_VehiclelistDao().loadAll();
    }


    public void deleEvaluating(){
        daoSession.getRecommend_EvaluatingDao().deleteAll();
    }
    public void addEvaluating(List<Recommend_Evaluating> data){
        deleEvaluating();
        daoSession.getRecommend_EvaluatingDao().insertInTx(data);
    }
    public List<Recommend_Evaluating> searchEvaluating(){
        return daoSession.getRecommend_EvaluatingDao().loadAll();
    }


    //删除
    //NewestList
    public void deleteNewestList() {
        daoSession.getRecommend_NewslistDao().deleteAll();
    }

    //添加
    public void addNewestList(List<Recommend_Newslist> data) {
        deleteNewestList();
        daoSession.getRecommend_NewslistDao().insertInTx(data);
    }
    //查询
    public List<Recommend_Newslist> seacherNewestList() {
        return daoSession.getRecommend_NewslistDao().loadAll();
    }


    public void deleteFocusimgList(){
        daoSession.getRecommend_FocusingDao().deleteAll();
    }

    public void addFocusimgList(List<Recommend_Focusing> data){
        deleteFocusimgList();
        daoSession.getRecommend_FocusingDao().insertInTx(data);
    }

    public List<Recommend_Focusing> seacherFocusimgList(){
        return daoSession.getRecommend_FocusingDao().loadAll();
    }


    public void deleteNews(){
        daoSession.getRecommend_NewsDao().deleteAll();
    }

    public void addNews(List<Recommend_News> data){
        deleteNews();
        daoSession.getRecommend_NewsDao().insertInTx(data);
    }

    public List<Recommend_News> seacherNews(){
        return daoSession.getRecommend_NewsDao().loadAll();
    }


    public void deleteTotal(){
        daoSession.getForum_totalDao().deleteAll();
    }

    public void addTotal(List<Forum_total> data){
        deleteTotal();
        daoSession.getForum_totalDao().insertInTx(data);
    }

    public List<Forum_total> seacherTotal(){
        return daoSession.getForum_totalDao().loadAll();
    }


    public void deleteTechnologylist(){
        daoSession.getRecommend_TechnologylistDao().deleteAll();
    }

    public void addTechnologylist(List<Recommend_Technologylist> data){
        deleteTechnologylist();
        daoSession.getRecommend_TechnologylistDao().insertInTx(data);
    }

    public List<Recommend_Technologylist> seacherTechnologylist(){
        return daoSession.getRecommend_TechnologylistDao().loadAll();
    }
    public void deleteReduceDealer(){
        daoSession.getReduceDealerDao().deleteAll();
    }

    public void addReduceDealer(ReduceDealer data){
        deleteReduceDealer();
        daoSession.getReduceDealerDao().insertInTx(data);
    }

    public void deleteReduceCarlist(){
        daoSession.getReduceCarListDao().deleteAll();
    }

    public ReduceDealer searchReduceDealer(long id){
        QueryBuilder<ReduceDealer> qb = daoSession.getReduceDealerDao().queryBuilder();
        qb.where(ReduceDealerDao.Properties.Dealer_id.eq(id));
        qb.limit(1);
        if (qb.list().size() > 0){
            return qb.list().get(0);

        }else return  null;
    }

    public void addReduceCarlist(List<ReduceCarList> data, List<ReduceDealer> reduceDealer){

        for (int i = 0; i < reduceDealer.size(); i++) {
            long id = daoSession.getReduceDealerDao().insert(reduceDealer.get(i));
            ReduceCarList list = data.get(i);
            list.setDealer_id(id);
            daoSession.getReduceCarListDao().insert(list);
        }
    }

    public List<ReduceCarList> seacherReduceCarlist(){
        List<ReduceCarList> list =daoSession.getReduceCarListDao().loadAll();
        for (int i = 0; i <list.size() ; i++) {
            ReduceDealer deal = searchReduceDealer(list.get(i).getDealer_id());
            if (deal != null){
                list.get(i).setDealer(deal);
            }
        }

        return list;
    }

    //删除
    public void deleteFamousList() {
        daoSession.getFamousListDao().deleteAll();
    }

    //添加
    public void addFamousList(List<FamousList> data) {
        deleteFamousList();
        daoSession.getFamousListDao().insertInTx(data);
    }
    //查询
    public List<FamousList> seacherFamousList() {
        return daoSession.getFamousListDao().loadAll();
    }

    public void deleJingXuanList(){
        daoSession.getJingXuanListDao().deleteAll();
    }
    public void addJingXuanList(List<JingXuanList> data){
        deleJingXuanList();
        daoSession.getJingXuanListDao().insertInTx(data);
    }
    public List<JingXuanList> seacherJingXuanList(){
        return daoSession.getJingXuanListDao().loadAll();
    }



}
